#!/bin/bash

LIVEPATCH_TOOL=`which livepatch`
KPATCHRTOOL_RECORD_SRC="./.kpatchrtool_record"
# store all current patch records 
G_PATCH_RECORD=
#########################################################
#    Description: fn_usage
#    Input:  
#    Return: 0-success
#########################################################
function fn_usage()
{
        cat << EOF
Usage:
    kpatchrtool [*OPTIONS*]

Options:
    -h | help
        show help information
    -v | --version
        show version
    -w | --recover 
        recover hotpatch in order of records
    -l | --load <patch path>
        Load and record hotpatch
    -a | --active <patch name>
        active kpatch in order of records
    -r | --rollback <patch name>
        rollback/deactive to a hotpatch record in order of records
    -d | --delete <patch name>
        delete/unload to a hotpatch record in order of records
    -q | --query
        Query hot patch information
    -s | --status
        record: Query status of hot patch records
    -c | --close
        Turn off the auto start service, which recovers the hot patch according to the records
    -o | --open
        Turn on the auto start service, which recovers the hot patch according to the records

EOF

return 0
}
#########################################################
#    Description: fn_prepare
#    Input:  
#    Return: 0-success,1-failed
#########################################################
function fn_prepare()
{
    return 0
}

#########################################################
#    Description: fn_do_clean, remove tmp file
#    Input: 
#    Return: 0-success
#########################################################
function fn_do_clean()
{
    return 0
}

#########################################################
#    Description: fn_do_recover, recover hot patch records
#    Input: 
#    Return: 0-success
#########################################################
function fn_do_recover(){
    #TODO recover hot patch records
    return 0
}


#########################################################
#    Description: fn_call_livepatch
#    Input:  
#       $@:livepatch param
#    Return: 0-success,1-failed
#########################################################
function fn_call_livepatch()
{
    local param=$@
    $LIVEPATCH_TOOL $@
    return $? 
}


#########################################################
#    Description:  verify input param
#    Input:  
#        $@: input param
#    Return: 0-success,1-failed
#########################################################
function fn_verify_input()
{
    local input_param=$@
    local ret=
    if [ $# -lt 1 ]; then
        echo "error: missing param, please check it"
        fn_do_clean
        fn_usage
        exit 1
    fi

    return 0
}


#########################################################
#    Description: fn_process_input
#    Input:  
#        $@: input param
#    Return: 0-success,1-failed
#########################################################
function fn_process_input()
{
    local ret=
    case "$1" in
        -h|--help)
            fn_usage
            fn_do_clean
            exit 0
            ;;
        -v|--version)
            echo "kpatchrtool version: 0.0.0"
            fn_do_clean
            exit 0
            ;;
        -w|--recover)
            fn_do_recover $@
            ret=$?
            fn_do_clean
            exit $ret
            ;;
        -l|--load)
            fn_call_livepatch $@
            ret=$?
            #TODO: patch record(load patch)
            fn_do_clean
            exit $ret
            ;;
        -a|--active)
            fn_call_livepatch $@
            ret=$?
            #TODO: patch record(active patch)
            fn_do_clean
            exit $ret
            ;;
        -r|--rollback)
            fn_call_livepatch $@
            ret=$?
            #TODO: patch record(rollback patch)
            fn_do_clean
            exit $ret
            ;;
        -d|--delete)
            fn_call_livepatch $@
            ret=$?
            #TODO: patch record(delete patch)
            fn_do_clean
            exit $ret
            ;;
        -q|--query)
            fn_call_livepatch $@
            ret=$?
            fn_do_clean
            exit $ret
            ;;
        -s|--status)
            fn_show_status $@
            ret=$?
            # TODO: display patch record(delete patch)
            fn_do_clean
            exit $ret
            ;;
        -c|--close)
            # TODO: close_selfstarting_sevice
            # fn_close_selfstarting_sevice $@
            # ret=$?
            fn_do_clean
            exit 0
            ;;
        -o|--open)
            # TODO: open_selfstarting_sevice
            # fn_open_selfstarting_sevice $@
            # ret=$?
            fn_do_clean
            exit 0
            ;;
        *)
            echo "error: params is invalid, please check it."
            fn_do_clean
            fn_usage
            exit 1
            ;;
    esac
}
#########################################################
#    Description:  main
#    Input:  
#    Return: 0-success,1-failed
#########################################################
function fn_main()
{
    fn_verify_input $@
    if [ $? -ne 0 ]; then
        fn_do_clean
        return 1
    fi

    fn_process_input $@
    if [ $? -ne 0 ]; then
        fn_do_clean
        return 1
    fi    

    fn_do_clean

    return 0

}
G_NUM=`pidof -x kpatchrtool | wc -w`
if [ $G_NUM -gt 0 ];then
    echo "[$0]someone is using, please try again later."
    exit 1
fi

fn_prepare
fn_main $@
exit $?